// 实现这个项目的构建任务
const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/Yikhan'
        },
        {
          name: 'About',
          link: 'https://weibo.com/Yikhan'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/Yikhan'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

module.exports = (grunt) => {
  grunt.initConfig({
    sass: {
      options: {
        sourceMap: true,
        implementation: sass
      },
      main: {
        files: [
          {
            expand: true,
            src: [ 'assets/styles/*.scss' ],
            dest: 'dist/',
            ext: '.css',
            cwd: 'src'
          }
        ]
      }
    },
    copy: {
      main: {
        files: [
          // includes files within path
          {
            expand: true,
            src: [ 'public/**' ],
            dest: 'dist/',
            filter: 'isFile'
          }
        ]
      }
    },
    babel: {
      options: {
        sourceMap: true,
        presets: [ '@babel/preset-env' ]
      },
      main: {
        files: [
          {
            expand: true,
            src: [ 'assets/scripts/*.js' ],
            dest: 'dist/',
            cwd: 'src'
          }
        ]
      }
    },
    web_swig: {
      options: {
        swigOptions: {
          cache: false
        },
        getData: function (tpl) {
          return { ...data }
        }
      },
      main: {
        files: [
          {
            expand: true,
            src: [ '**/*.html' ],
            dest: 'dist/',
            ext: '.html',
            cwd: 'src'
          }
        ]
      }
    },
    useref: {
      // specify which files contain the build blocks
      html: 'dist/**/*.html',
      // explicitly specify the temp directory you are working in
      // this is the the base of your links ( "/" )
      temp: '.'
    },
    watch: {
      js: {
        files: [ 'src/assets/scripts/*.js' ],
        tasks: [ 'babel' ]
      },
      css: {
        files: [ 'src/assets/styles/*.scss' ],
        tasks: [ 'sass' ]
      }
    }
  })

  loadGruntTasks(grunt) // 自动加载所有的 grunt 插件中的任务

  grunt.registerTask('default', [
    'sass',
    'babel',
    'copy',
    'web_swig',
    'useref',
    'concat',
    'uglify',
    'cssmin'
  ])
}
